package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommentsService {
    @DubboReference
    private CommentApi commentApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private RedisTemplate redisTemplate;

    //查询所有评论
    public PageResult findComments(String movementId, Integer page, Integer pagesize) {
        //1.设置评论的标识符
        Integer commentType = 2;
        //2.查询互动表需要知道两个参数 动态id和互动类型;
        List<Comment> list = commentApi.findComments(movementId, commentType, page, pagesize);
        //3.判断一个评论列表是否为空
        if (CollUtil.isEmpty(list)) {
            //4.如果为空就返回一个新的分页对象
            return new PageResult();
        }
        //5.如果不为空，就获取到评论人的id
        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        //6.根据id集合查询出所有人的信息
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds);
        //7.循环动态集合，每一个comment构建一个vo
        ArrayList<CommentVo> vos = new ArrayList<>();
        for (Comment comment : list) {
            //8.获取互动的操作人
            Long userId = comment.getUserId();
            //9.获取互动操作人的信息
            UserInfo info = map.get(userId);
            //10.根据得到的互动人的信息和comment构建返回的vo对象
            CommentVo vo = CommentVo.init(info, comment);

            //从redis中查询点赞状态
            String redisKey="COMMENT_INTERACT_"+vo.getId();
            String hashKey="COMMENT_LIKE_"+UserHolder.getUserId();
            Boolean hasKey = redisTemplate.opsForHash().hasKey(redisKey, hashKey);
            //进行判断
            if (hasKey) {
                vo.setHasLiked(1);
            }

            vos.add(vo);
        }
        //11.返回对象
        return new PageResult(page, pagesize, 0l, vos);
    }

    //发布评论
    public void saveComment(String movementId, String content) {
        //1.先将评论内存保存到互动表当中
        //1.1 设置互动类型为
        Integer commentType = 2;
        //1.2 创建一个互动对象
        Comment comment = new Comment();
        //1.3 给vo对象设置值， 这里还有一个PublishUserId留在api层设置
        comment.setPublishId(new ObjectId(movementId));  //动态Id
        comment.setCommentType(commentType);            //动态类型
        comment.setContent(content);                    //评论内容
        comment.setUserId(UserHolder.getUserId());      //操作人ID
        comment.setCreated(System.currentTimeMillis()); //发表时间
        //2.进行保存
        Integer count = commentApi.save(comment);
        //3.
        System.out.println("评论总数是:" + count);

    }

    //评论的点赞数
    public Integer like(String id) {
        //调用api来处理
       Integer count= commentApi.commentLike(id);
        //像redis中保存标记
        String redisKey="COMMENT_INTERACT_"+id;
        String hashKey="COMMENT_LIKE_"+UserHolder.getUserId();
        redisTemplate.opsForHash().put(redisKey,hashKey,"1");
       return count;
    }

    //取消对评论的点赞
    public Integer disLike(String id) {
        //调用service来处理
        Integer count= commentApi.disLike(id);
        //删除redis中保存标记
        String redisKey="COMMENT_INTERACT_"+id;
        String hashKey="COMMENT_LIKE_"+UserHolder.getUserId();
        redisTemplate.opsForHash().delete(redisKey, hashKey);
        //返回数据
        return count;
    }
}
